$(function() {
    $( "#datepicker").datepicker();
    //汉化
    $.datepicker.regional['zh-CN'] = {
		currentText: '今天',
		monthNames: ['一月','二月','三月','四月','五月','六月',
		'七月','八月','九月','十月','十一月','十二月'],
		monthNamesShort: ['一','二','三','四','五','六',
		'七','八','九','十','十一','十二'],
		dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
		dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],
		dayNamesMin: ['日','一','二','三','四','五','六'],
		dateFormat: 'yy-mm-dd', 
		firstDay: 1,
		isRTL: false};
		$.datepicker.setDefaults($.datepicker.regional['zh-CN']);
		});
		
	$('#type').change(function(){
		if($(this).val()=='图片新闻'){
			$('#picnews').show();
		}else{ 
			$('#picnews').hide();
		}
	});

	
	//初始化编辑器
	var ue = UE.getEditor('editor');	
	ue.ready(function() {		   
	   ue.setContent('hello');
	   var html = ue.getContent();						    
	});
//表单1验证
	$().ready(function(e) {
		
		$("#myForm1").validate({
			debug:true,
			rules: {
				headline: {
					required: true,
					minlength: 2,
					maxlength:50
				},
				date: {
					required: true,
					date:true
				},
				source: {
					required: true
				},
				uploader: {
					required: true
				}
			},
			messages: {
				headline: '新闻标题需要2到50个字',
				date: '请选择日期',
				source: '请填写新闻来源',
				uploader: '请填写上传者名字'
			},
			submitHandler:function(){
				var param=$('#myForm1').serialize();	
				if($('#type').val()=='图片新闻'){
				var base64=base64_data.replace(/^data:image\/(png|jpeg);base64,/, "");
				base64=encodeURIComponent(base64); 
				//var base64=base64_data;			
				param+='&picnews='+base64;			
				}else{
					param+='&picnews=null';
				}								
				console.log(param);
				
				$.ajax({
					url:'../homeinformation/submithomeinformation',
					type:'POST',
					data:param,
					async:true,
					success:function(data){
						//window.location.href='../homeinformation/index'; 

					}, 
					error:function(e){
						console.log(e);
					}
				});  
			
				
			},
			
			
		});
		
		var base64_data;
		
		//上传图片预览压缩
		$('#xFile').change(function(){	
			 var MAX_HEIGHT = 500;
			var file=this.files[0];			
			if(file.size>5*1024*1024){
				alert('请选择10m以下的图片');
				return;
			}
		    if(!/image\/\w+/.test(file.type)){  
		        alert("看清楚，这个需要图片！");  
		        return;  
		    }  
		    var reader=new FileReader();
		     reader.onload = (function(file) {	    			    	
		            return function(e) {	
		            	base64_data=this.result;
		              $('#result').html('<img width="300" src="'+ this.result +'" alt="'+ file.name +'" /><div class="fileName"><span class="label label-info">'+file.name+'</span></div>');	
		                render(this.result);      	           	
		            };
		        })(file);
		      //读取文件内容
		        reader.readAsDataURL(file);
		      
		       function compress(image){		    
				     var canvas = document.getElementById("canvas");
				     // 如果高度超标
				     if(image.height > MAX_HEIGHT) {
				     // 宽度等比例缩放 *=
				     image.width *= MAX_HEIGHT / image.height;
				     image.height = MAX_HEIGHT;
				     }		  
				     console.log(image.width,image.height);
				     var ctx = canvas.getContext("2d");
				     // canvas清屏
				     ctx.clearRect(0, 0, canvas.width, canvas.height);
				     // 重置canvas宽高
				     canvas.width = image.width;
				     canvas.height = image.height;
				     //设置背景为白色，否则会变黑
				     ctx.fillStyle = "#fff";
				     ctx.fillRect(0, 0, canvas.width, canvas.height);
				     // 将图像绘制到canvas上		     
				     ctx.drawImage(image, 0, 0, image.width, image.height);    
				     // !!! 注意，image 没有加入到 dom之中
				     canvas.toDataURL('image/jpeg', 0.8);					   
				     	     
				    };		     		
		     		     
		     function render(src){			    	 
				 var image = new Image();	
				 image.src =src;   		   
			     // 绑定 load 事件处理器，加载完成后执行
			     image.onload=compress(image);
		         	         		          			      	      		     
		     }
		    
		});
		
	});
	
	